<template>
  <div class="app-container brandInformation">
    <el-form :model="selectForm" ref="selectForm" :inline="true" label-width="120px">
      <el-form-item label="品牌名称：">
        <el-input size="small" v-model="selectForm.brandName" placeholder="请输入品牌名称" clearable @keyup.enter.native="getList"/>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="getList" v-hasPermi="['system:brandInformation:search']">搜索</el-button>

        <el-button size="mini" type="success" icon="el-icon-plus" @click="add" v-hasPermi="['system:brandInformation:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list.rows" border v-loading="loading">
      <el-table-column align="center" type="index" label="序号" width="55"/>
      <el-table-column label="品牌名称" prop="brandName" align="center"/>
      <el-table-column label="品牌介绍" prop="brandDesc" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建人" prop="creatorName" align="center"/>
      <el-table-column label="操作" align="center" fixed="right" width="200px">
        <template slot-scope="scope">
          <el-button size="mini" type="success" icon="el-icon-edit" @click="edit(scope.row)"
                     v-hasPermi="['system:brandInformation:edit']">修改</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="del(scope.row)" v-hasPermi="['system:brandInformation:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="list.total?list.total:0"
      :page.sync="selectForm.pageNum"
      :limit.sync="selectForm.pageSize"
      @pagination="getList"
    />
    <el-dialog :close-on-click-modal="false" :title="addForm.id?'编辑品牌':'新增品牌'" :visible.sync="addVisible" width="500px">
      <el-form class="form" :model="addForm" ref="addForm" :inline="true" label-width="110px">
        <el-form-item label="品牌名称：" prop="brandName" :rules="[{required: true, message: '请输入品牌名称', trigger: 'blur'}]">
          <el-input size="small" v-model="addForm.brandName" placeholder="请输入品牌名称" clearable/>
        </el-form-item>
        <el-form-item label="品牌介绍：" prop="brandDesc">
          <el-input type="textarea" :rows="3" v-model="addForm.brandDesc" placeholder="请输入品牌介绍"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false" size="mini">取消</el-button>
        <el-button type="primary" @click="onSumbit" size="mini">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {deviceBrand, deviceBrandDel, deviceBrandAdd, deviceBrandEdit} from "@/api/basicInformation/index";

  export default {
    name: "brandInformation.vue",
    data() {
      return {
        selectForm: {
          brandName: '',
          isDelete: 1,
          pageNum: 1,
          pageSize: 10,
        },
        addForm: {
          brandDesc: '',
          brandName: '',
        },
        loading: false,
        list: '',
        addVisible: false,
      }
    },
    created() {
      this.getList();
    },
    methods: {
      async getList() {
        this.loading = true;
        let res = await deviceBrand(this.selectForm);
        this.list = res;
        this.loading = false;
      },
      add() {
        delete this.addForm.id
        Object.assign(this.$data.addForm, this.$options.data().addForm)
        this.addVisible = true
      },
      edit(row) {
        this.addForm = {
          id: row.id,
          brandDesc: row.brandDesc,
          brandName: row.brandName,
        };
        this.addVisible = true;
      },
      onSumbit() {
        this.$refs['addForm'].validate(async (valid) => {
          if (valid) {
            this.addForm.id ? await deviceBrandEdit(JSON.stringify(this.addForm)) : await deviceBrandAdd(JSON.stringify(this.addForm));
            this.addForm.id ? this.$message.success('编辑成功') : this.$message.success('新增成功');
            this.addVisible = false;
            this.getList();
          } else {
            // this.$message.info('数据不能为空')
            return false;
          }
        });
      },
      del(row) {
        this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await deviceBrandDel(row.id);
          this.$message.success('删除成功');
          this.getList();
        }).catch(() => {
          this.$message.info('取消删除')
        });
      },

    }
  }
</script>

<style scoped lang="scss">
  .brandInformation {
    .blue {
      color: #2196F3;
      cursor: pointer;
      padding: 0 5px;
    }

    .form {
      width: 350px;
    }
  }
</style>
